package health.lm.com.shop.fragment;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.lm.sjy.titlebar.widget.CommonTitleBar;
import com.luck.picture.lib.decoration.RecycleViewDivider;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import health.lm.com.component_base.base.mvp.fragment.XListFragment;
import health.lm.com.router.ARouterConstant;
import health.lm.com.shop.R;
import health.lm.com.shop.R2;
import health.lm.com.shop.adapter.PeopleChainAdapter;
import health.lm.com.shop.entity.PeopleChainMultiEntity;
import health.lm.com.shop.presenter.PeopleChainPresenter;

/**
 * Created by Android Studio .
 * Project：hlian health.lm.com.shop.activity
 * Date：2019/8/14 16:35
 * Introduce：人脉连首页
 */
@Route(path = ARouterConstant.HLPeopleChainFragment)
public class PeopleChainFragment extends XListFragment<PeopleChainPresenter> {
    @BindView(R2.id.title_bar)
    CommonTitleBar mTitleBar;
    @BindView(R2.id.rv_news)
    RecyclerView mRvNews;
    @BindView(R2.id.smart_refresh)
    SmartRefreshLayout mSmartRefresh;
    private PeopleChainAdapter mChainAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.layout_bar_refresh_list;
    }

    @Override
    public PeopleChainPresenter newP() {
        return new PeopleChainPresenter();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.recyclerView = mRvNews;
        super.rlRefreshLayout = mSmartRefresh;
        mTitleBar.getLeftImageButton().setVisibility(View.GONE);
        mTitleBar.getCenterTextView().setText("人脉链");
        mTitleBar.getRightImageButton().setImageDrawable(ContextCompat.getDrawable(context, R.drawable.renmai_nav_icon_message));
        mTitleBar.setListener((v, action, extra) -> {
            if (action == CommonTitleBar.ACTION_RIGHT_BUTTON) {//消息列表
                gotoActivity(ARouterConstant.HLPrivateMessageActivity);
            }
        });
        mChainAdapter = new PeopleChainAdapter(new ArrayList<>(), getFragmentManager());
        super.adapter = mChainAdapter;
        super.initData(savedInstanceState);
        mRvNews.setLayoutManager(new LinearLayoutManager(context));
        mRvNews.setAdapter(mChainAdapter);
        mRvNews.addItemDecoration(new RecycleViewDivider(context, LinearLayoutManager.HORIZONTAL
                , 20, ContextCompat.getColor(context, R.color.color_nav_f7f7f7)));

        getP().getData();
    }

    /**
     * 加载数据
     *
     * @param isRefresh true 刷新 false 加载更多
     * @param layout    头部布局
     * @param page      页数
     * @param pageSize  每页数量
     */
    @Override
    public void loadListData(boolean isRefresh, Object layout, int page, int pageSize) {
        getP().getData();
    }

    public void setNewData(List<PeopleChainMultiEntity> pNewData) {
        if (isRefresh) mSmartRefresh.finishRefresh();
        mChainAdapter.setNewData(pNewData);
    }
}
